#include <NewSoftSerial.h>

#include "./protocol.h"
#include "./status.h"
#include "./display.h"

#define TEMP1 1
#define TEMP2 2

protocol Master;
status Status;
display LCD;


/*oggetto New Soft Serial v11*/
NewSoftSerial nss(8,9);  //RX:8 TX:9 nss with PC

/*variabili per temporizzazione in main loop*/
unsigned long int now=0;
unsigned long int sendingTemp=0;
//unsigned long int sendingTemp2=-5000;

int desireTemp=0;

/********************************************************************
/* Setup
/********************************************************************/
void setup(){

	pinMode(6, INPUT);
	pinMode(7, OUTPUT);
	
	pinMode(8, INPUT);
	pinMode(9, OUTPUT);
	
	Master.start(9600);
	nss.begin(9600);
	delay(50);
	LCD.start(9600);
	delay(50);
	LCD.initDisplay();
	delay(50);
	nss.println("HELLO, I'M JARVIS, YOUR PERSONAL BUTLER :) ");

	now=millis();
	sendingTemp=millis();
	desireTemp=TEMP1;
}



/********************************************************************
/* Main Loop
/********************************************************************/
void loop(){
	now=millis();
	if(nss.available()>0){
		Master.readDomesticPC((char)nss.read());
	}
	
	if(Serial.available()>0){
		Master.readNetwork((char)Serial.read());
		//nss.println((char)Serial.read());
	}
	
	if(now>(sendingTemp+30000)){	//ogni 30 secondi invio verso pc di T1 e T2 sfalsati
		switch(desireTemp){			//invio delle temperature sfalsate di 30" per non congestionare il traffico.
			case TEMP1:
				//Serial.println("ecco1");
				//invio t1
				nss.println(Master.sendInternalTemp());
				desireTemp=TEMP2;
				break;
			case TEMP2:
				//Serial.println("ecco2");
				//invio t1
				nss.println(Master.sendExternalTemp());
				desireTemp=TEMP1;
				break;
		}
		sendingTemp=millis();		
	}
	
}


